"""
AI分析任务数据模型
"""
from sqlalchemy import Column, String, Integer, ForeignKey, JSON, DateTime, Text
from sqlalchemy.orm import relationship
from models.base import BaseModel


class AITask(BaseModel):
    """AI分析任务模型"""
    __tablename__ = "ai_tasks"
    
    camera_id = Column(Integer, ForeignKey("cameras.id"), nullable=False, comment="摄像头ID")
    algorithm_name = Column(String(100), nullable=False, comment="算法名称")
    algorithm_version = Column(String(20), comment="算法版本")
    task_type = Column(String(50), nullable=False, comment="任务类型")
    parameters = Column(JSON, comment="算法参数")
    status = Column(String(20), default="pending", comment="任务状态")
    priority = Column(Integer, default=5, comment="优先级(1-10)")
    gpu_id = Column(Integer, comment="分配的GPU ID")
    started_at = Column(DateTime, comment="开始时间")
    completed_at = Column(DateTime, comment="完成时间")
    error_message = Column(Text, comment="错误信息")
    
    # 关联关系
    camera = relationship("Camera", back_populates="ai_tasks")
    results = relationship("AIResult", back_populates="task", cascade="all, delete-orphan")
    
    def __repr__(self):
        return f"<AITask(id={self.id}, algorithm='{self.algorithm_name}', status='{self.status}')>"